package one.stand.controller;

import com.jmp.base.*;
import com.jmp.base.dto.*;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.ResultModel;
import one.stand.request.CompanyCustomerLableRequest;
import one.stand.response.CompanyCustomerLableResponse;
import one.stand.service.CompanyCustomerLableService;
import one.stand.service.sys.UserContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 */
@RestController
public class CompanyCustomerLableController implements LabelApi {

    @Autowired
    private CompanyCustomerLableService companyCustomerLableService;
    @Autowired
    private UserContentService userContentService;

    @Override
    public ResultModel<LabelSaveResponse> save(@RequestBody LabelSaveRequest request) {
        CompanyCustomerLable companyCustomerLableModel = new CompanyCustomerLable();
        companyCustomerLableModel.setCompanyId(request.getCompanyId());
        companyCustomerLableModel.setUserId(request.getUserId());
        companyCustomerLableModel.setLableName(request.getLableName());
        companyCustomerLableModel.setLableColor(request.getLableColor());
        companyCustomerLableModel.setParentId(request.getParentId());
        Integer labelId = companyCustomerLableService.addLable(companyCustomerLableModel);
        LabelSaveResponse response = LabelSaveResponse.builder()
                .labelId(labelId)
                .build();
        return ResultModel.success(response);
    }

    @Override
    public ResultModel delete(@RequestBody LabelDeleteRequest request) {
        companyCustomerLableService.delLable(request.getLableId());
        return ResultModel.success();
    }

    @Override
    public ResultModel update(@RequestBody LabelUpdateRequest request) {
        companyCustomerLableService.updLable(request.getLableId(), request.getLableName(), request.getLableColor(), request.getParentId());
        return ResultModel.success();
    }

    @Override
    public ResultModel relate(@RequestBody LabelRelateRequest request) {
        userContentService.updateLabel(request.getType(), request.getTypeId(), request.getLabelIds());
        return ResultModel.success();
    }

    /**
     * 获取客户标签树
     */
    @PostMapping("companyCustomerLable/getTree")
    public ResultModel<List<CompanyCustomerLableResponse>> getTree(@RequestBody CompanyCustomerLableRequest request) {
        return companyCustomerLableService.getTree(request);
    }
}
